package com.fourforfo.fourmall.search.web;

import com.fourforfo.common.constant.AuthConstant;
import com.fourforfo.common.utils.HttpUtils;
import com.fourforfo.fourmall.search.service.MallSearchService;
import com.fourforfo.fourmall.search.vo.SearchParam;
import com.fourforfo.fourmall.search.vo.SearchResult;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 */
@Controller
public class SearchController {

    @Autowired
    MallSearchService mallSearchService;

    @GetMapping({"/list.html"})
    public String listPage(SearchParam param, Model model, HttpServletRequest request,HttpSession httpSession){

        String url = HttpUtils.getRequestURLByForwordedHost(request);
        model.addAttribute(Conf.REDIRECT_URL,url);

        String queryString = request.getQueryString();
        param.setQueryString(queryString);

        Object attribute = httpSession.getAttribute(AuthConstant.LOGIN_USER);
        System.out.println("搜索页的session为："+attribute);
        SearchResult result = mallSearchService.search(param);

        model.addAttribute("result",result);
        return "list";
    }

}
